package com.justnow.offer;

/**
 * @author justnow
 * Created on 2020-09-04
 * Description
 *
 * 一个由小写字母组成的字符串，可以看成一些同一字符的最大碎片组成的。
 * 例如，"aaabbaaac"是由下面碎片组成的'aaa' 'bb' 'c。现在给定一个字符串，请你帮助
 * 计算这个字符串的所有碎片的平均长度是多少
 *
 * "aaabbaaac"
 *
 * 2
 *
 * (3 2 3 1) / 4 = 2;
 */
public class Bilibili03 {
    public int GetFragment (String str) {
        // write code here
        if (str == null && str.length() <= 0) {
            return 0;
        }
        int start = 0, end = 0;
        char[] chars = str.toCharArray();
        int sum = 0;
        int number = 0;
        while (start <= end && end < chars.length) {
            while (end < chars.length && chars[start] == chars[end]) {
                end++;
            }
            sum += end - start;
            number++;
            start = end;
        }
       return sum / number;
    }

    public static void main(String[] args) {
        Bilibili03 bilibili03 = new Bilibili03();
        int n = bilibili03.GetFragment("aabaaac");
        System.out.println(n);
    }
}
